'''------------
登记一个新的患者
通过新增检查，上传本地CT数据
查看新上传的CT
关闭查看，退出登录
------------'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import os,time,content,random

usrname = '13980617516'
pasword = '10000'
t1 = (1976, 1, 1, 0, 0, 0, 0, 0, 1)
t2 = (2020, 12, 31, 23, 59, 59, 0, 0, 1)
start = time.mktime(t1)
end = time.mktime(t2)
birth_year = random.randint(start, end)
date = time.localtime(birth_year)
year = date[0]
month = date[1]
day = date[3]
str1 = random.sample('美嘎嘎嘎漂亮兰花莲花百合花清洁文宇珊', 3)
pat_name = ("").join(str1)
print(year, month, day)

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(5)
# 登录
driver.get('https://dxt.yayi360.com/#/')
driver.find_element(By.XPATH, '//*[@placeholder="请输入手机号"]/input').send_keys(usrname)
driver.find_element(By.XPATH, '//*[@placeholder="请输入密码"]/input').send_keys(pasword)
driver.find_element(By.CLASS_NAME, 'zh_postForm').click()
driver.implicitly_wait(5)
#登记，填写患者名、性别、出生日期
driver.find_element(By.XPATH, '//*[@class="el-date-interval-top fac-date"]/..//button[2]').click()#登记
driver.find_element(By.XPATH, '//*[@placeholder="请输入患者姓名"]/input').send_keys(pat_name)#患者名
driver.find_element(By.XPATH, '//*[@for="sex"]/../div[1]/div/label[1]/span[1]').click()#性别
driver.find_element(By.XPATH, '//*[@for="date"]/../div/div/div[1]/input').send_keys(year)#年
driver.find_element(By.XPATH, '//*[@for="date"]/../div/div/div[2]/input').send_keys(month)#月
driver.find_element(By.XPATH, '//*[@for="date"]/../div/div/div[2]/input').send_keys(Keys.DOWN)
driver.find_element(By.XPATH, '//*[@for="date"]/../div/div/div[3]/input').send_keys(day)#日
driver.find_element(By.XPATH, '//*[@for="date"]/../div/div/div[3]/input').send_keys(Keys.DOWN)
#确保生成日期是1位数时使用下键确保输入格式
driver.implicitly_wait(5)
driver.find_element(By.XPATH, '//*[contains(@class,"buttons-box")]/button[1]').click()#确定登记
#classname是唯一，但是比较长的时候，用contains来进行模糊定位：xpath = "//标签名[contains(@属性, '属性值')]"
driver.implicitly_wait(5)
driver.find_element(By.ID, "YS-sy").click()
#搜索患者
driver.find_element(By.XPATH, '//*[@placeholder="患者姓名/编号"]/input').send_keys(pat_name)
driver.find_element(By.XPATH, '//*[@class="el-date-interval-top fac-date"]/..//button[1]').click()
time.sleep(2)
#获取搜索结果中显示的条数
result = driver.find_element(By.XPATH, '//*[@class="yg-pagination__total"]').text
print(result)
if result == '共 0 条':
    print("没有找到对应患者")
    driver.refresh()
else:
    print("找到患者")
    driver.find_element(By.XPATH, '//*[contains(@class,"is-underline")]/span').click()#进入详情页
    driver.implicitly_wait(2)
    driver.find_element(By.XPATH, '//span[@class="cp2"]').click()#新增检查
    driver.find_elements_by_class_name("n_c_item")[0].click()#选择上传CT
    driver.find_element(By.XPATH, '//p[@class="col-8"]/../button[1]').click()#选择上传影像
    os.system("G:\\test_team\\upload-CT.au3")
    driver.implicitly_wait(120)
    state = driver.find_element(By.CLASS_NAME, 'reUpload').get_attribute('class')
    if state == 'reUpload':
        print("上传CT成功")
        driver.find_element(By.XPATH, '//*[contains(@class,"yg-diaglog zxc-ct")]/div/div[1]/span[2]').click()#关闭上传窗口
        driver.refresh()
        driver.find_element(By.XPATH, '//*[@class="selectButton select_h"]/div[1]/button[1]').click()#点击查看
        time.sleep(30)
        driver.find_element(By.XPATH, '//*[@id="pane-default"]/div/div[1]/div[4]/div/div[2]/span').click()# 关闭总导航线
        time.sleep(2)
        line = driver.find_element(By.XPATH, '//*[@data-text="隐藏导航线"]').get_attribute('class')#获取轴向面的导航线状态
        if line == "hide-line":
            print("打开CT成功")
            driver.get_screenshot_as_file("G:/jietu/test.png")
            driver.find_element(By.XPATH, '//*[@class="nsel close-btn"]/i').click()#关闭CT页面
            driver.implicitly_wait(5)
        else:
            print("打开CT失败")
            driver.forward()
            driver.implicitly_wait(5)
    else:
        print("上传CT失败")
        driver.find_element(By.XPATH, '//*[contains(@class,"yg-diaglog zxc-ct")]/div/div[1]/span[2]').click()#关闭上传窗口
#退出登录
driver.find_element(By.CLASS_NAME, "yg-dropdown-link").click()
driver.find_elements_by_class_name("yg-dropdown-menu__item")[3].click()
driver.find_element(By.XPATH, '//*[@id="yg_msgbox_dom"]/div/div[3]/input').click()
time.sleep(5)
if driver.current_url == 'https://dxt.yayi360.com/#/':
    print('退出成功')
else:
    print('退出失败')
driver.quit()